package com.ktjiaoyu.springsecuritydemo.service;

import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Resource
    PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //根据提供用户名查询数据库有没有对应用户信息
        if(!"admin".equals(username)){
            throw new UsernameNotFoundException("用户名不存在");
        }
        //加密密码
        String password=passwordEncoder.encode("123456");
        return new User(username,password,
                AuthorityUtils.commaSeparatedStringToAuthorityList("Admin,normal,ROLE_Abc,ROLE_normal,insert,select"));
    }
}
